import { ADD_CAR_COUNT, ADD_CAR_LIST } from "./mutations-types";

export default {
  handleCart(context, payload) {
    return new Promise((resolve, reject) => {
      let currentIndex = 0;
      let product = context.state.cartList.find((item, index) => {
        if (item.iid === payload.iid) {
          currentIndex = index;
          return true;
        } else {
          return false;
        }
      });
      if (product) {
        // state.cartList[currentIndex].count += 1;
        context.commit(ADD_CAR_COUNT, context.state.cartList[currentIndex]);
        resolve("当前的商品数量+1");
      } else {
        payload.count = 1;
        // state.cartList.push(payload);
        context.commit(ADD_CAR_LIST, payload);
        resolve("添加了新的商品");
      }
    });
  }
};
